package com.wanghai.carwashing.common.helper;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * HTTP工具类
 * @author 王海
 */
public class HttpHelper {
    
    public static HttpResult get(String urlString) throws IOException, URISyntaxException{
        URL url = new URL(urlString); 
        URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(uri);
        httpGet.addHeader("charset","utf-8");
        CloseableHttpResponse response = httpClient.execute(httpGet);
        int status = response.getStatusLine().getStatusCode();
        String content = EntityUtils.toString(response.getEntity(),"UTF-8");
        response.close();
        httpClient.close();
        HttpResult httpResult = new HttpResult();
        httpResult.setStatus(status);
        httpResult.setEntity(content);
        return httpResult;
    }
    
    public static HttpResult post(String url,Map<String,Object> paramMap){
        return null;
    }
    
    public static HttpResult post(String url,Map<String,Object> paramMap,Map<String,File> fileParamMap){
        return null;
    }
}
